package com.demomaster.quickcompat.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * 复合型recycler
 * @author squirrel桓
 * @date 2022/4/13.
 * description：
 */
public class MulAudioGroupRecyclerAdapter<VH extends RecyclerView.ViewHolder> extends QuickRecyclerAdapter<VH> {

    Context context;
    List<MulItemRecycler> mulItemRecyclers;
    //MulItemManager mulItemManager;
    public MulAudioGroupRecyclerAdapter(Context context, List<MulItemRecycler> mulItemRecyclers) {
        this.context = context;
        this.mulItemRecyclers = mulItemRecyclers;
    }
//    public MulRecyclerAdapter(Context context, MulItemManager mulItemManager) {
//        this.context = context;
//        this.mulItemManager = mulItemManager;
//        this.mulItemRecyclers = mulItemManager.getItemList();
//    }

    public void notifyDataSetChanged(List<MulItemRecycler> mulItemRecyclers){
        this.mulItemRecyclers.clear();
        this.mulItemRecyclers.addAll(mulItemRecyclers);
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
//        if(this.mulItemManager!=null) {
//            return mulItemManager.getItemCount();
//        }
        return mulItemRecyclers==null?0:mulItemRecyclers.size();
    }
    @Override
    public int getItemViewType(int position) {
        //QdLog.d("MulRecyclerAdapter","getItemViewType="+position);
        return position;//super.getItemViewType(position);
    }

    @NotNull
    @Override
    public VH onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
//        if(this.mulItemManager!=null) {
//            MulItemRecycler mulItemRecycler = mulItemManager.getItem(viewType);
//            View view = mulItemRecycler.onCreateView(context,null);
//            return (VH) new ChildHolder(context,parent,mulItemRecycler,view);
//        }
        //QdLog.d("MulRecyclerAdapter","onCreateViewHolder="+viewType);
        View view = mulItemRecyclers.get(viewType).onCreateView(context,null);
        return (VH) new ChildHolder(context,parent,mulItemRecyclers.get(viewType),view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull VH holder, int position) {
        super.onBindViewHolder(holder, position);
        //QdLog.d("MulRecyclerAdapter","onBindViewHolder="+position);
        ((ChildHolder)holder).bind(holder.itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }

    public static class ChildHolder extends RecyclerView.ViewHolder {
        MulItemRecycler mulItemRecycler;
        Context context;
        public ChildHolder(Context context, ViewGroup parent, @NotNull MulItemRecycler mulItemRecycler, View view) {
            super(view);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            this.context = context;
            this.mulItemRecycler = mulItemRecycler;
        }
        public void bind(View itemView) {
            mulItemRecycler.bindView(context,itemView);
        }
    }

    public static abstract class MulItemRecycler {
        public abstract View onCreateView(Context context,ViewGroup parent);
        public abstract void bindView(Context context,View itemView);
    }

}
